home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / ooav3 / ooav.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-06-12  |  7.4 KB  |  298 lines

  1. {
  2.         Object-Oriented Archive-viewer, version 3
  3.         ─────────────────────────────────────────
  4.         This Object-Oriented Archive-viewer (OOAV) is copyright (c) by
  5.         Edwin Groothuis, MavEtJu software. You are free to use it
  6.         if you agree with these three rules:
  7.  
  8.         1. You tell me you're using this unit.
  9.  
  10.         2. You give me proper credit in the documentation. (Like:
  11.            "This program uses the Object-Oriented Archive-viewer
  12.             (c) Edwin Groothuis, MavEtJu software".
  13.  
  14.         3. If you make Archive-objects for other archive-types, don't
  15.            hesitate to inform me so I can add them to the unit and
  16.            redistribute it!
  17.  
  18.         That's all!
  19.  
  20.         How to use this unit:
  21.         ─────────────────────
  22.         (see also the file ArchTest.pas)
  23.  
  24.         - Declare a variable Arch of the       var Arch:TArchive;
  25.           type TArchive                        begin
  26.         - Call it's constructor                  Arch.Init;
  27.         - Tell the unit which file you           if not Arch.Name('TEST.ZIP')
  28.           want to view. This function            then begin
  29.           returns a boolean. If this               writeln('TEST.ZIP is not
  30.           boolean is false, then the                        a valid archive');
  31.           file couldn't be identified              exit;
  32.           as a valid archive.                    end;
  33.         - Just like the dos-functions            Arch.FindFirst(sr);
  34.           FindFirst and FindNext, you            while sr.Name<>'' do
  35.           can search through the archive.        begin
  36.           The parameter you give with it           writeln(sr.Name);
  37.           is one of the SearchRec-type.            Arch.FindNext(sr);
  38.           If there are no more files in          end;
  39.           this archive, sr.Name will be
  40.           empty. Valid fields are
  41.           sr.Name, sr.Size and sr.Time
  42.         - Call the destructor                    Arch.Done;
  43.                                                end;
  44.  
  45.         - You can call the function
  46.           IdentifyArchive() to see what
  47.           kind of archive you're dealing
  48.           with.
  49.  
  50.         What if you want to add more archive-types
  51.         ──────────────────────────────────────────
  52.         - Add the unit name in the second Uses-statement.
  53.         - Find out how to identify it and add that algoritm
  54.           to the IdentifyArchive()-function. Please choose a
  55.           unique and no-nonsens character to return.
  56.         - Add it to the IdentifyArchive()-case in TArchive.Name.
  57.         - Create a FindFirst-method and FindNext-method for this
  58.           object.
  59.         - That's it! Simple, isn't it? (If it isn't, please see the
  60.           files ZipView, ArjView and others for examples ;-)
  61.  
  62.         Author:
  63.         ───────
  64.         Edwin Groothuis          email:
  65.         Johann Strausslaan 1     edwing@stack.urc.tue.nl (valid until 10-94)
  66.         5583ZA Aalst-Waalre      Edwin_Groothuis@p1.f205.n284.z2.gds.nl
  67.         The Netherlands          2:284/205.1@fidonet
  68.                                  115:3145/102.1@pascal-net
  69.  
  70.  
  71. }
  72.  
  73. unit      OOAV;
  74.  
  75. interface
  76.  
  77. uses      Dos;
  78.  
  79. {
  80.   General Archive, which is the father of all the specific archives. See
  81.   OOAVZip, OOAVArj and others for examples.
  82. }
  83. type      PGeneralArchive=^TGeneralArchive;
  84.           TGeneralArchive=object
  85.                             _FArchive:file;
  86.  
  87.                             constructor Init;
  88.                             destructor Done;virtual;
  89.  
  90.                             procedure FindFirst(var sr:SearchRec);virtual;
  91.                             procedure FindNext(var sr:SearchRec);virtual;
  92.                           end;
  93.  
  94. {
  95.   TArchive is the object you're working with. See the documentation at the
  96.   begin of this file for more information
  97. }
  98. type      PArchive=^TArchive;
  99.           TArchive=object
  100.                      constructor Init;
  101.                      destructor Done;
  102.  
  103.                      function  Name(const n:string):boolean;
  104.  
  105.                      procedure FindFirst(var sr:SearchRec);
  106.                      procedure FindNext(var sr:SearchRec);
  107.  
  108.                    private
  109.                      _Name:string;
  110.                      _Archive:PGeneralArchive;
  111.                    end;
  112.  
  113.  
  114. function  IdentifyArchive(const Name:string):char;
  115.  
  116. implementation
  117.  
  118. uses      Objects,Strings,
  119.           OOAVZip,OOAVArj,OOAVLzh,OOAVArc,OOAVZoo;
  120.  
  121.  
  122. function  IdentifyArchive(const Name:string):char;
  123. {
  124.   returns:
  125.     '?': unknown archive
  126.     'A': Arj-archive;
  127.     'Z': Zip-archive
  128.     'L': Lzh-archive
  129.     'C': Arc-archive
  130.     'O': Zoo-archive
  131. }
  132. var       f:file;
  133.           a:array[0..10] of char;
  134.           bc:word;
  135.           s:string;
  136.           OldFileMode:byte;
  137. begin
  138.   if Name='' then
  139.   begin
  140.     IdentifyArchive:='?';
  141.     exit;
  142.   end;
  143.  
  144.   OldFileMode:=FileMode;
  145.   FileMode:=0;
  146.   assign(f,Name);
  147.   {$I-}reset(f,1);{$I+}
  148.   FileMode:=OldFileMode;
  149.   if IOresult<>0 then
  150.   begin
  151.     IdentifyArchive:='?';
  152.     exit;
  153.   end;
  154.  
  155.   blockread(f,a,sizeof(a),bc);
  156.   close(f);
  157.   if bc=0 then
  158.   begin
  159.     IdentifyArchive:='?';
  160.     exit;
  161.   end;
  162.  
  163.   if (a[0]=#$60) and (a[1]=#$EA) then
  164.   begin
  165.     IdentifyArchive:='A';  { ARJ }
  166.     exit;
  167.   end;
  168.  
  169.   if (a[0]='P') and (a[1]='K') then
  170.   begin
  171.     IdentifyArchive:='Z';  { ZIP }
  172.     exit;
  173.   end;
  174.  
  175.   if a[0]=#$1A then
  176.   begin
  177.     IdentifyArchive:='C';  { ARC }
  178.     exit;
  179.   end;
  180.  
  181.   if (a[0]='Z') and (a[1]='O') and (a[2]='O') then
  182.   begin
  183.     IdentifyArchive:='O';  { ZOO }
  184.     exit;
  185.   end;
  186.  
  187.   s:=Name;
  188.   for bc:=1 to length(s) do
  189.     s[bc]:=upcase(s[bc]);
  190.   if copy(s,pos('.',s),4)='.LZH' then
  191.   begin
  192.     IdentifyArchive:='L';  { LZH }
  193.     exit;
  194.   end;
  195.  
  196.   IdentifyArchive:='?';
  197. end;
  198.  
  199.  
  200. constructor TGeneralArchive.Init;
  201. begin
  202.   Abstract;
  203. end;
  204.  
  205.  
  206. destructor TGeneralArchive.Done;
  207. begin
  208. end;
  209.  
  210.  
  211. procedure TGeneralArchive.FindFirst(var sr:SearchRec);
  212. begin
  213.   Abstract;
  214. end;
  215.  
  216.  
  217. procedure TGeneralArchive.FindNext(var sr:SearchRec);
  218. begin
  219.   Abstract;
  220. end;
  221.  
  222.  
  223. constructor TArchive.Init;
  224. begin
  225.   _Name:='';
  226.   _Archive:=nil;
  227. end;
  228.  
  229.  
  230. destructor TArchive.Done;
  231. begin
  232.   if _Archive<>nil then
  233.   begin
  234.     close(_Archive^._FArchive);
  235.     Dispose(_Archive,Done);
  236.   end;
  237. end;
  238.  
  239.  
  240. function  TArchive.Name(const n:string):boolean;
  241. var       sr:SearchRec;
  242.           OldFileMode:byte;
  243. begin
  244.   if _Archive<>nil then
  245.   begin
  246.     close(_Archive^._FArchive);
  247.     Dispose(_Archive,Done);
  248.     _Archive:=nil;
  249.   end;
  250.  
  251.   Name:=false;
  252.   _Name:=n;
  253.   Dos.FindFirst(_Name,anyfile,sr);
  254.   if DosError<>0 then
  255.     exit;
  256.  
  257.   case IdentifyArchive(_Name) of
  258.     '?': exit;
  259.     'A': _Archive:=New(PArjArchive,Init);
  260.     'Z': _Archive:=New(PZipArchive,Init);
  261.     'L': _Archive:=New(PLzhArchive,Init);
  262.     'C': _Archive:=New(PArcArchive,Init);
  263.     'O': _Archive:=New(PZooArchive,Init);
  264.   end;
  265.  
  266.   OldFileMode:=FileMode;
  267.   FileMode:=0;
  268.   Assign(_Archive^._FArchive,n);
  269.   {$I-}reset(_Archive^._FArchive,1);{$I+}
  270.   FileMode:=OldFileMode;
  271.   if IOresult<>0 then
  272.   begin
  273.     Dispose(_Archive);
  274.     exit;
  275.   end;
  276.  
  277.   Name:=true;
  278. end;
  279.  
  280.  
  281. procedure TArchive.FindFirst(var sr:SearchRec);
  282. begin
  283.   FillChar(sr,sizeof(sr),0);
  284.   if _Archive=nil then
  285.     exit;
  286.   _Archive^.FindFirst(sr);
  287. end;
  288.  
  289. procedure TArchive.FindNext(var sr:SearchRec);
  290. begin
  291.   FillChar(sr,sizeof(sr),0);
  292.   if _Archive=nil then
  293.     exit;
  294.   _Archive^.FindNext(sr);
  295. end;
  296.  
  297. end.
  298.